package dk.itu.mobile.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * @author niebje
 *
 */
@XmlRootElement(name = "task")
public class Task implements Serializable {

	private static final long serialVersionUID = 1L;

	@XmlAttribute
	public String id;
	
	@XmlAttribute
	public String name;
	
	@XmlAttribute
	public String date;
	
	@XmlAttribute
	public Status status;
	
	@XmlAttribute
	public boolean required;
	
	public String description;
	public String attendants;
	transient private List <User> users;
	public String conditions;
	public String responses;

	public Task() {
		//Empty constructor for marshalling
		users = new ArrayList<User>();
	}

	public Task(String name, String id, String date, String description) {
		this.name = name;
		this.id = id;
		this.date = date;
		this.description = description;
		this.status = Status.NOT_EXECUTED;
		this.required = false;
		this.conditions = "";
		this.responses = "";
	}

	public Task(String name, String id, String date, String description, List <User> users) {
		this(name,id,date,description);
		this.users = users;
	}

	@XmlTransient
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List <User> users) {
		this.users = users;
		attendants = "";
		for (User item : users) {
			attendants += item.name +", ";
		}
	}

	public boolean getRequired() {

		return this.required;

	}
	
	public void setRequired(Boolean b) {
		this.required = b;
	}
	
	public boolean addCondition(String s ) {
		
		String[] con = this.conditions.split(",");
		
		for (String c : con) { if (s.equalsIgnoreCase(c)) return false;}
		this.conditions += ","+s;
		return true;
	}

	public String[] getConditions() {

		String[] con = this.conditions.split(",");	
		return con;	
	}
	
	public String toString() {
		String value = "Task: \n";
		
    	value += "   Id: " + id == null? "no id found":id + "\n";
		value += "   name: " + name == null? "no name found":name + "\n";
		value += "   date: " + date.toString() == null? new Date().toString():date.toString() + "\n";
		value += "   status: " + status == null? "no status found":status + "\n";
		value += "   required: " + required + "\n";
		value += "   description: " + description == null? "no description found":description + "\n";
		value += "   attendants: " + attendants== null? "no attendants found":attendants + "\n";
		value += "   conditions: " + conditions + "\n";
		value += "   responses: " + responses + "\n\n";

//				
		return value;
	}
		
}
